
package com.mybatisflex.test;

import com.mybatisflex.core.MybatisFlexBootstrap;
import com.mybatisflex.core.query.QueryWrapper;
import org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseBuilder;
import org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseType;

import javax.sql.DataSource;
import java.util.Date;
import java.util.List;

import static com.mybatisflex.core.query.QueryMethods.max;
import static com.mybatisflex.test.table.Account2TableDef.ACCOUNT2;
import static com.mybatisflex.test.table.AccountTableDef.ACCOUNT;

public class AccountSample {


    /**
     * 初始化数据源
     * @return
     */
    private static MybatisFlexBootstrap buildBootstrap(){
        DataSource dataSource = new EmbeddedDatabaseBuilder()
                .setType(EmbeddedDatabaseType.H2)
                .addScript("schema.sql")
                .addScript("data.sql")
                .build();

        MybatisFlexBootstrap bootstrap = MybatisFlexBootstrap.getInstance()
                .setDataSource(dataSource)
//              .setLogImpl(StdOutImpl.class) //日志输出配置
                .addMapper(AccountMapper.class)
                .start();
        return bootstrap;
    }


    public static void main(String[] args) {
        MybatisFlexBootstrap bootstrap=buildBootstrap();
        //得到mapper
        AccountMapper accountMapper = bootstrap.getMapper(AccountMapper.class);

        //查询 ID 为 1 的数据
        Account account = accountMapper.selectOneById(1);
        System.out.println(account);


        //新增 1 条数据
        Account newAccount = new Account();
        newAccount.setUserName("李四");
        newAccount.setAge(18);
        newAccount.setBirthday(new Date());
        accountMapper.insert(newAccount);
        System.out.println("newAccount Id: " + newAccount.getId());


        //查询全部数据
        System.out.println(accountMapper.selectAll());


        //基本查询 where and or
        QueryWrapper queryWrapper = QueryWrapper.create()
                .select()
                .where(ACCOUNT.ID.eq(2));
        System.out.println(accountMapper.selectOneByQuery(queryWrapper));


        //聚合函数和别名的使用
        QueryWrapper query = QueryWrapper.create()
                .select(ACCOUNT.USER_NAME,
                        max(ACCOUNT.BIRTHDAY).as("birthday")
                ).groupBy(ACCOUNT.USER_NAME);

        System.out.println(accountMapper.selectListByQuery(query));


        //left join 表 到主表对象
        QueryWrapper query2 = QueryWrapper.create()
                .select().from(ACCOUNT)
                .leftJoin(ACCOUNT2).on(ACCOUNT.ID.eq(ACCOUNT2.ID))
                .where(ACCOUNT.ID.eq(1));
        System.out.println(accountMapper.selectListByQuery(query2));



        //join 返回值设置到新对象Account3  select tb_account.user_name,tb_account2.age from tb_account join tb_account2 on tb_account.id=tb_account2.id where tb_account.id=1
        QueryWrapper query3 = QueryWrapper.create()
                .select(ACCOUNT.USER_NAME.as("user_name3"),ACCOUNT2.AGE.as("age3"))
                .from(ACCOUNT)
                .join(ACCOUNT2).on(ACCOUNT.ID.eq(ACCOUNT2.ID))
                .where(ACCOUNT.ID.eq(1));
        List<Account3> results = accountMapper.selectListByQueryAs(query3, Account3.class);
        System.out.println(results);


        //MyBatis 原生注解  xml的不举例了 也是支持的
        System.out.println(accountMapper.selectById(1));

    }
}
